package com.gopay.common.user.dao.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.cust.UserInfoConstants;
import com.gopay.common.domain.UserInfo;
import com.gopay.common.domain.dc.DcUserCardInfo;
import com.gopay.common.user.dao.DCUserCardInfoDao;

/**
 * 动态口令卡 
 * @author zhangzg
 * @date 2015年9月1日 下午5:17:05
 * www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("dCUserCardInfoDao")
public class DCUserCardInfoDaoImpl extends BaseDAOImpl<DcUserCardInfo, Serializable> implements DCUserCardInfoDao{
	
		/**
		 * 修改动态口令卡的密码字段
		 * @return
		 */
		@Override
        public void updateUserCardInfoForPwd(){
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("date", new Date());
			super.updateByHql(getUpdateAotoSql(), map);
		}
		
		/**
		 * 获取绑定的动态口令卡
		 * @return
		 */
		@Override
        public List<DcUserCardInfo> queryUserCardInfos(){
			return super.findByHql(getqueryCardsSql());
		}
		
		
		/**
		 * 获取绑定的动态口令卡sql
		 */
		private String getqueryCardsSql() {
			return "from DcUserCardInfo dc where dc.unbundingStat='0'";
		}

		/**
		 * 跑修改sql 只要有动态口令卡就处理一下
		 */
		private String getUpdateAotoSql(){
			//update DcUserCardInfo dc set dc.pwdLockStat=0,set dc.tokenErrorNum=0,set dc.lastUpdTs=:date where dc.pwdLockStat=1
			StringBuilder sql=new StringBuilder("update DcUserCardInfo dc ");
			sql.append(" set dc.pwdLockStat='0',dc.tokenErrorNum='0',dc.lastUpdTs=:date ");
			return sql.toString();
		}

        @Override
        public DcUserCardInfo getUserCardInfoByCustId(String custId) {
            String hql = "from DcUserCardInfo where custId = :custId";
            
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("custId", custId);
            List<DcUserCardInfo> dcUserCardInfoList = this.findByHql(hql, param);
            if (dcUserCardInfoList == null || dcUserCardInfoList.size() == 0) {
                return null;
            }
            return dcUserCardInfoList.get(0);
        }

        @Override
        public DcUserCardInfo getUserCardInfoByUserId(String userId) {
            String hql = "from DcUserCardInfo where userId = :userId";
            
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("userId", userId);
            List<DcUserCardInfo> dcUserCardInfoList = this.findByHql(hql, param);
            if (dcUserCardInfoList == null || dcUserCardInfoList.size() == 0) {
                return null;
            }
            return dcUserCardInfoList.get(0);
        }

}
